﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Notewp.Model;

namespace Notewp.ViewModel
{
    public class Connect
    {
        //DB_NoteContext la 1 class thuoc linq.cs
        private static string con = databaseContext.ConnectionString;// biến con read/write 
        //Tạo Database

        public static void CreatDatabase()
        {
            using (var context = new databaseContext(con))
            {

                context.CreateIfNotExists();

            }

        }
        //Xóa database
        public static void DeleteDataBase()
        {
            using (var context = new databaseContext(con))
                if (context.DatabaseExists())
                {
                    context.DeleteDatabase();
                }
        }
        //==========================TAG TABLE===============================
        //them tagid
        public void AddTagID(string name_tag)
        {
            try
            {

                using (var context = new databaseContext(con))
                    if (context.DatabaseExists())
                    {
                        var tag = new TagID()
                        {
                            NameTag = name_tag
                        };
                        context.TagID.InsertOnSubmit(tag);
                        context.SubmitChanges();

                    }
            }
            catch(Exception ex)
            {
                
            }

        }
        //cap nhat tagid
        public void UpdateTagID(int tag_id,string new_content)
        {
            try
            {
            using (var context =new databaseContext(con))
            {
                var sua=context.TagID.Single(r=>r.TagID1==tag_id);
                sua.NameTag=new_content;
                context.SubmitChanges();
            }
            }
            catch(Exception ex)
            {

            }
        }
        //Xoa tagid
        public void DeleteTagID(int tag_id)
        {
            try
            {
            using (var context =new databaseContext(con))
            {
                var tag_de=context.TagID.Single(r=>r.TagID1==tag_id);
                context.TagID.DeleteOnSubmit(tag_de);
                context.SubmitChanges();
            }
            }
            catch(Exception ex)
            {

            }
        
        }
        //==================END TAG=========================
        


        //===================tblNote=========================
        //add moi note
        public void AddNote(bool check_img, string _tieude, string _noidung, DateTime _ngaytao, string _thoigianluu, int _tagID, bool _danhdau, byte[] _image)
        {
            
            using (var context = new databaseContext(con))
                {
                    if (check_img)
                    {
                
                            var Note = new TblNote()
                            {
                                TieuDe = _tieude,
                                NoiDung = _noidung,
                                NgayTao = _ngaytao,
                                ThoiGianLuu = _thoigianluu,
                                TagID = _tagID,
                                DanhDau = _danhdau,
                                Image = _image
                            };
                            context.TblNote.InsertOnSubmit(Note);
                            
                        }
                    else
                    {
                
                            var Note = new TblNote()
                            {
                                TieuDe = _tieude,
                                NoiDung = _noidung,
                                NgayTao = _ngaytao,
                                ThoiGianLuu = _thoigianluu,
                                TagID = _tagID,
                                DanhDau = _danhdau
                                
                            };
                            context.TblNote.InsertOnSubmit(Note);
                            
                
                    }
                    context.SubmitChanges();
                
                }

        }
        //update note
        public void UpdateNote(int Id,string _tieude, string _noidung, DateTime _ngaytao, string _thoigianluu, int _tagID, bool _danhdau)
        {
            try
            {
                using (var context = new databaseContext(con))
                {
                    var sua = context.TblNote.Single(r => r.ID == Id);
                    sua.TieuDe = _tieude;
                    sua.NoiDung = _noidung;
                    sua.NgayTao = _ngaytao;
                    sua.ThoiGianLuu = _thoigianluu;
                    sua.TagID =  _tagID;
                    sua.DanhDau = _danhdau;
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {

            }
        }
        //delete note
        public void DeleteNote(int id)
        {
            try
            {
                using (var context = new databaseContext(con))
                {
                    var tag_de = context.TblNote.Single(r =>r.ID==id);
                    context.TblNote.DeleteOnSubmit(tag_de);
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {

            }
        }
        
    }
}
